//
//  Home.m
//  Speedater
//
//  Created by NgoThang on 12/28/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Home.h"
#import "Login.h"
#import "Start.h"
#import "PrivacyPolicy.h"
#import "AGB.h"
#import "Imprint.h"
#import "UserProfile.h"
#import "ForgotPassword.h"
#import "Contact.h"
#import "Mail.h"
#import "GMap.h"
#import "Register.h"
#import "ChangePassword.h"
#import "ContactSearch.h"
#import "ContactDetail.h"
#import "MailCompose.h"
#import "MailDetail.h"
#import "AppDelegate.h"

@interface Home (Private)

- (void)cleanup;

@end

@implementation Home (Private)

- (void)cleanup
{
    self.registerButton = nil;
    self.loginButton = nil;
}

@end


@implementation Home

@synthesize registerButton, loginButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    //NSLog(@"HOME -> dealloc");
    [self cleanup];
    [super dealloc];
}

#pragma mark - Custom functions

-(IBAction)registerButtonClick:(id)sender
{
    Register *regis = [[Register alloc] init];
    [self.navigationController pushViewController:regis animated:YES];
    [regis release];
}

-(IBAction)loginButtonClick:(id)sender
{
    Login *login = [[Login alloc] init];
    [self.navigationController pushViewController:login animated:YES];
    [login release];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //NSLog(@"HOME -> viewDidLoad");
    [super viewDidLoad];
    
    // set background image
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    
    // remove username
    [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"username"];
}

- (void)viewDidUnload
{
    //NSLog(@"HOME -> viewDidUnload");
    [self cleanup];
    [super viewDidUnload];
}

- (void)autoMakeVC:(NSString *)className
{
    if ([className isEqualToString:@"Home"]) {
        return;
    }
    
    if ([className isEqualToString:@"Login"]) {
        Login *login = [[Login alloc] init];
        [self.navigationController pushViewController:login animated:NO];
        [login release];
        return;
    }
    
    if ([className isEqualToString:@"Register"]) {
        Register *reg = [[Register alloc] init];
        [self.navigationController pushViewController:reg animated:NO];
        [reg release];
        return;
    }
    
    if ([className isEqualToString:@"ForgotPassword"]) {
        ForgotPassword *forgotPassword = [[ForgotPassword alloc] init];
        [self.navigationController pushViewController:forgotPassword animated:NO];
        [forgotPassword release];
        return;
    }
    
    if ([className isEqualToString:@"Start"]) {
        Start *start = [[Start alloc] init];
        [self.navigationController pushViewController:start animated:NO];
        [start release];
        return;
    }
    
    if ([className isEqualToString:@"PrivacyPolicy"]) {
        PrivacyPolicy *privacyPolicy = [[PrivacyPolicy alloc] init];
        [self.navigationController pushViewController:privacyPolicy animated:NO];
        [privacyPolicy release];
        return;
    }
    
    if ([className isEqualToString:@"AGB"]) {
        AGB *agb = [[AGB alloc] init];
        [self.navigationController pushViewController:agb animated:NO];
        [agb release];
        return;
    }
    
    if ([className isEqualToString:@"Imprint"]) {
        Imprint *imprint = [[Imprint alloc] init];
        [self.navigationController pushViewController:imprint animated:NO];
        [imprint release];
        return;
    }
    
    if ([className isEqualToString:@"UserProfile"]) {
        UserProfile *userProfile = [[UserProfile alloc] init];
        [self.navigationController pushViewController:userProfile animated:NO];
        [userProfile release];
        return;
    }
    
    if ([className isEqualToString:@"ChangePassword"]) {
        ChangePassword *changePassword = [[ChangePassword alloc] init];
        [self.navigationController pushViewController:changePassword animated:NO];
        [changePassword release];
        return;
    }
    
    if ([className isEqualToString:@"Radar"]) {
        return;
    }
    
    if ([className isEqualToString:@"GMap"]) {
        GMap *gmap = [[GMap alloc] init];
        gmap.friendUsername = nil;
        [[[[self.tabBarController.viewControllers objectAtIndex:1] topViewController] navigationController] pushViewController:gmap animated:NO];
        [gmap release];
        return;
    }
    
    if ([className isEqualToString:@"Contact"]) {
        return;
    }
    
    if ([className isEqualToString:@"ContactSearch"]) {
        ContactSearch *contactSearch = [[ContactSearch alloc] init];
        [[[[self.tabBarController.viewControllers objectAtIndex:2] topViewController] navigationController] pushViewController:contactSearch animated:NO];
        [contactSearch release];
        return;
    }
    
    if ([className isEqualToString:@"ContactDetail"]) {
        ContactDetail *contactDetail = [[ContactDetail alloc] init];
        contactDetail.friendUsername = [[NSUserDefaults standardUserDefaults] stringForKey:@"contact_friend"];
        [[[[self.tabBarController.viewControllers objectAtIndex:2] topViewController] navigationController] pushViewController:contactDetail animated:NO];
        [contactDetail release];
        return;
    }
    
    if ([className isEqualToString:@"Mail"]) {
        return;
    }
    
    if ([className isEqualToString:@"MailCompose"]) {
        MailCompose *mailCompose = [[MailCompose alloc] init];
        mailCompose.username = [[NSUserDefaults standardUserDefaults] stringForKey:@"mail_username"];
        mailCompose.subject = [[NSUserDefaults standardUserDefaults] stringForKey:@"mail_subject"];
        [[[[self.tabBarController.viewControllers objectAtIndex:3] topViewController] navigationController] pushViewController:mailCompose animated:NO];
        [mailCompose release];
        return;
    }
    
    if ([className isEqualToString:@"MailDetail"]) {
        MailDetail *mailDetail = [[MailDetail alloc] init];
        mailDetail.mailDetailId = [[NSUserDefaults standardUserDefaults] stringForKey:@"mail_detail_id"];
        [[[[self.tabBarController.viewControllers objectAtIndex:3] topViewController] navigationController] pushViewController:mailDetail animated:NO];
        [mailDetail release];
        return;
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    //NSLog(@"HOME -> viewWillAppear");
    //NSLog(@"HOME -> viewControllers: %@", self.navigationController.viewControllers);
    self.navigationController.navigationBar.hidden = YES;
    
    // if auto login happen
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"])
    {
        // turn off
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"auto_login"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"last_state"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        // generate login form with auto login params
        Login *login = [[Login alloc] init];

        [self.navigationController pushViewController:login animated:NO];
        [login release];
        
        // generate start form
        Start *start = [[Start alloc] init];
        [self.navigationController pushViewController:start animated:YES];
        [start release];
    }
    
    // if crash occur
    NSString *last_state = [[NSUserDefaults standardUserDefaults] stringForKey:@"last_state"];
    //NSLog(@"++%@", last_state);
    NSArray *arr = [last_state componentsSeparatedByString:@"$"];
    if ([arr count]) {
        // gps restore
        AppDelegate *d = [[UIApplication sharedApplication] delegate];
        d.lat = [[NSUserDefaults standardUserDefaults] floatForKey:@"gps_lat"];
        d.lon = [[NSUserDefaults standardUserDefaults] floatForKey:@"gps_lon"];
        
        // default tab
        if ([[arr objectAtIndex:0] integerValue]) {
            [self autoMakeVC:@"Login"];
            [self autoMakeVC:@"Start"];
        }
        
        // restore view controllers
        for (NSInteger i=1; i<[arr count]; i++) {
            [self autoMakeVC:[arr objectAtIndex:i]];
        }
        
        // synchronize
        [[NSUserDefaults standardUserDefaults] setValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"username"] forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"password"] forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"last_state"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        // view
        self.tabBarController.selectedIndex = [[arr objectAtIndex:0] integerValue];
    }
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
